package com.zhiche.lisa.bms.service.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.zhiche.lisa.bms.pojo.dto.driver.HycOrderData;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @author linbao
 * @date 2019-11-18
 */
@Component
@Slf4j
public class HycApiUtil {

    /**
     * 项目接口
     */
    @Value("${lisa.hyc.url}")
    private String hycUrl;

    /**
     * 超时时间
     */
    @Value("${lisa.socketTimeout}")
    private Integer socketTimeout;

    /**
     * 根据指令号获取数据
     */
    @Value("${lisa.hyc.queryDataByOrderCode}")
    private String queryDataByOrderCode;

    /**
     * 重新计价和推送的接口
     */
    @Value("${lisa.hyc.reComputePriceToOtm}")
    private String reComputePriceToOtm;


    /**
     * 慧运车根据指令号查询数据
     *
     * @param orderCodeList
     * @return
     * @throws BaseException
     */
    public List<HycOrderData> getHycDataByOrderCode(Collection<String> orderCodeList) throws BaseException {
        if (CollectionUtils.isEmpty(orderCodeList)) {
            return null;
        }
        String orderCodes = StringUtils.join(orderCodeList, ",");
        log.info("getHycDataByOrderCode 获取慧运车数据 json: {}", orderCodes);
        List<NameValuePair> params = Lists.newArrayList();
        params.add(new BasicNameValuePair("orderCodes", orderCodes));
        String result = HttpClientUtil.get(hycUrl + queryDataByOrderCode, null, params, socketTimeout);
        if (StringUtils.isNotBlank(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            Object data = jsonObject.get("data");
            if (Objects.nonNull(data)) {
                return JSONArray.parseArray(data.toString(), HycOrderData.class);
            }
        }
        return null;
    }

    /**
     * 慧运车根据指令号查询数据
     *
     * @param dwaybillIdList
     * @return
     * @throws BaseException
     */
    public List<HycOrderData> reComputePriceToOtm(Collection<String> dwaybillIdList) throws BaseException {
        if (CollectionUtils.isEmpty(dwaybillIdList)) {
            return null;
        }
        String dwaybillIdListStr = StringUtils.join(dwaybillIdList, ",");
        log.info("reComputePriceToOtm 重新计价 json: {}", dwaybillIdListStr);
        List<NameValuePair> params = Lists.newArrayList();
        params.add(new BasicNameValuePair("waybillId", dwaybillIdListStr));
        String result = HttpClientUtil.get(hycUrl + reComputePriceToOtm, null, params, socketTimeout);
        if (StringUtils.isNotBlank(result)) {
            log.info("reComputePriceToOtm 返回结果: {}", result);
        }
        return null;
    }
}
